﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using CCF20Model;

namespace CCF20Model.FaultHandler
{
    public interface IFaultHandler
    {
        Fault MyFault { get; set; }
        CCF20Entities ccf20Ent { get; set; }
        IFaultHandler PreviousHandler { get; set; }
        Vehicle MyVehicle { get; set; }
        Device MyDevice { get; set; }
        void ProcessRequest();
        //void SetNextHandler(IFaultHandler faultHandler);
        //void Next();
    }
    abstract public class FaultHandlerBase : IFaultHandler
    {
        public Fault MyFault { get; set; }
        public CCF20Entities ccf20Ent { get; set; }
        public IFaultHandler PreviousHandler { get; set; }
        public Vehicle MyVehicle { get; set; }
        public Device MyDevice { get; set; }
        public abstract void ProcessRequest();
        protected SMSModel smsModel = null;
        protected ModelFactory modelFactory = null;
        public void LogStart(string faultHandlerVersion="v1")
        {
            LoggerHelper.LogZICDebug(string.Empty);
            LoggerHelper.LogZICDebug(string.Format("***** This is the start of handling on IDC={0} FaultCode={1} Mileage ={2} Handler Version={3}************",
                                                        MyFault.IDCSimNumber,    
                                                        MyFault.Code,
                                                        MyFault.CurrentMileage,
                                                        faultHandlerVersion));
        }
        public void LogEnd()
        {
            LoggerHelper.LogZICDebug(string.Format("***** This is the end of handling on IDC={0} FaultCode={1} Mileage ={2}************",
                                                        MyFault.IDCSimNumber,
                                                        MyFault.Code,
                                                        MyFault.CurrentMileage));
            LoggerHelper.LogZICDebug(string.Empty);
        }
        protected void SendShortMessage(string smsType,string smsTitle)
        {
            //smsModel = GetSMSModel();
            SMSModel.SendShortMessage(MyVehicle, smsType,smsTitle,true);
        }
        protected void SendGPRSMessage(string smsType, string smsTitle)
        {
            //smsModel = GetSMSModel();
            SMSModel.SendGPRSMessage(MyVehicle, smsType, smsTitle);
        }
        protected SMSModel GetSMSModel()
        {
            if (smsModel == null)
            {
                modelFactory = new ModelFactory(ccf20Ent);
                smsModel = (SMSModel)modelFactory.CreateModel(ModelName.SMSModel);
            }
            return smsModel;
        }
        protected void CloseFault(string reason)
        {
            MyFault.IsActive = false;
            MyFault.IsConfirmed = false;
            MyFault.CloseDate = DateTime.Now;
            if (!string.IsNullOrEmpty(reason))
            {
                MyFault.CloseReason = reason;
            }
        }

    }

  
  

}
